package com.quwan.manager.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quwan.common.query.LambdaQueryWrapperX;
import com.quwan.common.result.PageResult;
import com.quwan.config.yml.OssConfig;
import com.quwan.entity.TCircleName;
import com.quwan.entity.TMemberAsk;
import com.quwan.manager.controller.dto.CircleNameDto;
import com.quwan.manager.service.ICircleNameService;
import com.quwan.mapper.TCircleNameMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.List;

/**
 * @author laozhang
 * @date 2022/3/17
 */
@Service
@RequiredArgsConstructor
public class CircleNameServiceImpl implements ICircleNameService {

    private final TCircleNameMapper circleNameMapper;
    private final OssConfig ossConfig;
    @Override
    public PageResult<CircleNameDto> getCircleNameListPage(int page, int size,Long circleTypeId, String circleName, Integer status) {

        Page<CircleNameDto> circleNamePage = circleNameMapper.selectCircleNameDtoList(new Page<>(page, size),circleTypeId,circleName,status);
        List<CircleNameDto> reslist = circleNamePage.getRecords();
        /* 开始遍历 */
        for (CircleNameDto ban : reslist) {
            if(ban.getHeaderUrl()!= null && ban.getHeaderUrl().length()>0){
                ban.setHeaderUrl(ossConfig.getHostname()+"/"+ban.getHeaderUrl());
            }
            if(ban.getBackUrl()!= null && ban.getBackUrl().length()>0){
                ban.setBackUrl(ossConfig.getHostname()+"/"+ban.getBackUrl());
            }
        }
        PageResult<CircleNameDto> result = new PageResult<CircleNameDto>()
                .setCurrent(circleNamePage.getCurrent())
                .setPages(circleNamePage.getPages())
                .setSize(circleNamePage.getSize())
                .setTotal(circleNamePage.getTotal())
                .setRecords(reslist);
        return result;
    }

    @Override
    public List<CircleNameDto> getCircleNameList(Long circleTypeId, String circleName, Integer status) {
        List<CircleNameDto> reslist = circleNameMapper.selectCircleNameDtoList(circleTypeId,circleName,status);
        /* 开始遍历 */
        for (CircleNameDto ban : reslist) {
            if(ban.getHeaderUrl()!= null && ban.getHeaderUrl().length()>0){
                ban.setHeaderUrl(ossConfig.getHostname()+"/"+ban.getHeaderUrl());
            }
            if(ban.getBackUrl()!= null && ban.getBackUrl().length()>0){
                ban.setBackUrl(ossConfig.getHostname()+"/"+ban.getBackUrl());
            }
        }
        return reslist;

    }

    @Override
    public boolean addCircleName(TCircleName circleName) {
        return circleNameMapper.insert(circleName) ==1;
    }

    @Override
    public boolean updateCircleName(TCircleName circleName) {
        return circleNameMapper.updateById(circleName) ==1;
    }

    @Override
    public TCircleName detailCircleName(Integer id) {
        return circleNameMapper.selectById(id);
    }

    @Override
    public boolean delCircleName(Integer id) {
        return circleNameMapper.deleteById(id) == 1;
    }
}
